use anyhow::Result;
use db::Db;
// use teloxide::prelude::*;
use matrix_sdk::{
    room::Room,
    ruma::events::room::message::{
        OriginalSyncRoomMessageEvent, Relation, RoomMessageEventContent,
    },
    Client,
};

pub async fn archive(
    ev: OriginalSyncRoomMessageEvent,
    _text: String,
    room: Room,
    _client: Client,
    db: Db,
) -> Result<()> {
    let relation = ev.content.relates_to.unwrap();
    if let Relation::Reply { in_reply_to } = relation {
        let event_id = in_reply_to.event_id.to_string();
        if let Some(userpost_id) = db.get_userpost_id(event_id).await? {
            log::info!("archiving");
            db.archive_post(userpost_id).await.unwrap();
            let content = RoomMessageEventContent::text_markdown("archived");
            room.send(content).await.unwrap();
            log::info!("archived post");
        }
    }
    Ok(())
}

// async fn archive_post(userpost_id: Uuid, db: &Db) -> Result<()> {
//     let time = OffsetDateTime::now_utc();
//     db.lock()
//         .await
//         .deref()
//         .query(
//             "UPDATE userposts SET read = true, read_at = $2 WHERE id = $1",
//             &[&userpost_id, &time],
//         )
//         .await?;

//     Ok(())
// }
